package com.example.bakend.service.Impl;

import com.example.bakend.mapper.BookMapper;
import com.example.bakend.pojo.Book;
import com.example.bakend.pojo.PageInfo;
import com.example.bakend.service.BookService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;


    @Override
    public PageInfo selectBooks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo();
        List<Book> books = bookMapper.selectBooks();
        Page<Book> page = (Page<Book>) books;
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setRows(page.getResult());
        return pageInfo;
    }

    @Override
    public List<Book> selectBooks() {
        return bookMapper.selectBooks();
    }

    @Override
    public Book selectBook(Integer id) {
        return bookMapper.selectBook(id);
    }

    @Override
    public Integer insertBook(Book book) {
        return bookMapper.insertBook(book);
    }

    @Override
    public Integer updateBook(Book book) {
        return bookMapper.updateBook(book);
    }


    @Override
    public PageInfo getBooks(String title, String author, LocalDateTime publishDateBegin, LocalDateTime publishDateEnd,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo pageInfo = new PageInfo();
        List<Book> books = bookMapper.getBooks(title, author, publishDateBegin, publishDateEnd);
        Page<Book> page = (Page<Book>) books;
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setRows(page.getResult());
        return pageInfo;
    }

    @Override
    public Integer deleteBook(Integer id) {
        return bookMapper.deleteBook(id);
    }

    @Override
    public Integer deleteBooks(List<Integer> ids) {
        return bookMapper.deleteBooks(ids);
    }

    @Override
    public List<Book> getBooksForCategory(Integer categoriesId) {
        return bookMapper.getBooksForCategory(categoriesId);
    }
}
